package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketAdExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 广告页面查询
 * @author chengyi
 * @date 2022/10/12 10:35
 */
@Service//注册为容器中的组件
public class AdminAdServiceImpl implements AdminAdService{
    @Autowired
    MarketAdMapper adMapper;

    /**
     * 广告查询
     * @param info
     * @param name
     * @param content
     * @return
     */
    @Override
    public CommonData<MarketAd> list(BasePageInfo info,String name,String content) {
        //执行之前，使用pageHelper开启分页
        PageHelper.startPage(info.getPage(),info.getLimit());

        //创建example实例
        MarketAdExample example = new MarketAdExample();

        //排序
        example.setOrderByClause(info.getSort()+" "+info.getOrder());

        MarketAdExample.Criteria criteria = example.createCriteria();

        if(!StringUtil.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if(!StringUtil.isEmpty(content)){
            criteria.andContentLike("%"+content+"%");
        }

        //查询广告
        List<MarketAd> list = adMapper.selectByExample(example);
        PageInfo<MarketAd> pageInfo = new PageInfo<>(list);


        return CommonData.data(pageInfo);
    }


    /**
     * 广告添加
     * @param marketAd
     * @return
     */
    @Override
    public MarketAd create(MarketAd marketAd) {


        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        java.util.Date date1 = null;
        try {
            date1 = formatter.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketAd.setAddTime(date1);
        marketAd.setUpdateTime(date1);
        adMapper.insertSelective(marketAd);
        int id = marketAd.getId();
        MarketAd data = adMapper.selectByPrimaryKey(id);
        return data;

    }

    /**
     * 编辑广告
     * @param
     * @return
     */
    @Override
    public MarketAd update(MarketAd marketAd) {

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        java.util.Date date2 = null;
        try {
            date2 = formatter.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketAd.setUpdateTime(date2);
        adMapper.updateByPrimaryKey(marketAd);
        int id = marketAd.getId();
        MarketAd marketAd1 = adMapper.selectByPrimaryKey(id);
        return marketAd1;
    }

    @Override
    public void delete(MarketAd marketAd) {
        int id = marketAd.getId();
        adMapper.deleteByPrimaryKey(id);


    }
}
